package com.kgc.community.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.Exception;
import com.kgc.community.pojo.User;
import com.kgc.community.service.ExceptionService;
import com.kgc.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("repair")
@Controller
public class ExceptionController {

    @Autowired
    ExceptionService exceptionTypeService;
    @Autowired
    UserService userService;

    @RequestMapping("showInsertPage.html")
    public String showInsertPage(ModelMap modelMap,String url,Integer id){

            Exception exception = exceptionTypeService.searchById(id);
            modelMap.addAttribute("exception",exception);

            List<User> users = userService.searchByRole(2);

            modelMap.addAttribute("users",users);
        return url;
    }

    @RequestMapping("searchExceptionToPage.html")
    public String searchExceptionToPage(ModelMap modelMap
            ,String url
            ,Integer searchType//查询类型，既要查询那个条件，那个字段
            ,String userName//输入的查询条件
            ,Integer status//异常的状态
            ,Integer typeId//异常类型
            ,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
            ,@RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        Map<Integer,Object> map = new HashMap<>();
        if (searchType!=null && userName!=null && userName.length()>0 && searchType!=-1){
            map.put(searchType,userName);
            modelMap.addAttribute("userName",userName).addAttribute("searchType",searchType);
        }
        map.put(5,typeId);
        if (status!=null && status!=-1){
            map.put(4,status);
            modelMap.addAttribute("status",status);
        }

        PageInfo<Exception> pageInfo = exceptionTypeService.searchExceptionByCondition(map, pageNum, pageSize);

        for (Exception exception : pageInfo.getList()) {
            System.out.println(exception.getDescribes());
        }

        modelMap.addAttribute("pageInfo",pageInfo);

        return url;
    }

    @RequestMapping("insertException.action")
    @ResponseBody
    public Map<String,Object> insertException(ModelMap modelMap,Exception e){
        Map<String,Object> map = new HashMap<>();

        Integer res = exceptionTypeService.insertSelective(e);

        if (res>0){
            map.put("status",1);
            map.put("info","提交成功！");
        }else{
            map.put("status",0);
            map.put("info","提交失败！");
        }

        return map;
    }


    @RequestMapping("updateException.action")
    @ResponseBody
    public Map<String,Object> updateException(ModelMap modelMap
            , Exception e
            ,String start_time
            ,String beginHandle_time
            ,String endHandle_time) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (start_time!=null && start_time.length()>0){
            e.setStartTime(format.parse(start_time));
        }
        if (beginHandle_time!=null && beginHandle_time.length()>0){
            e.setBeginHandleTime(format.parse(beginHandle_time));
        }
        if (endHandle_time!=null && endHandle_time.length()>0){
            e.setEndHandleTime(format.parse(endHandle_time));
        }

        Map<String,Object> map = new HashMap<>();
        Integer res = exceptionTypeService.updateSelectiveById(e);

        if (res>0){
            map.put("status",1);
            map.put("info","提交成功！");
        }else{
            map.put("status",0);
            map.put("info","提交失败！");
        }

        return map;
    }


    @RequestMapping("deleteException.action")
    @ResponseBody
    public Map<String,Object> deleteException(ModelMap modelMap,String id){
        List<Integer> idList = new ArrayList<>();
        idList.add(-1);

        String[] arrays = id.split(",");
        if (arrays.length>0){
            for (String array : arrays) {
                idList.add(Integer.parseInt(array));
            }
        }
        Map<String,Object> map = new HashMap<>();

        Integer res = exceptionTypeService.deleteByIdList(idList);
        if (res>0){
            map.put("status",1);
            map.put("info","删除成功！");
        }else{
            map.put("status",0);
            map.put("info","删除失败！");
        }

        return map;
    }
}
